package org.offerproject.shortlink.admin.controller;

import lombok.RequiredArgsConstructor;
import org.offerproject.shortlink.admin.common.convention.result.Result;
import org.offerproject.shortlink.admin.common.convention.result.Results;
import org.offerproject.shortlink.admin.dto.req.UserLoginReqDTO;
import org.offerproject.shortlink.admin.dto.req.UserRegisterReqDTO;
import org.offerproject.shortlink.admin.dto.req.UserUpdateReqDTO;
import org.offerproject.shortlink.admin.dto.resp.UserLoginRespDto;
import org.offerproject.shortlink.admin.dto.resp.UserRespDto;
import org.offerproject.shortlink.admin.service.UserService;
import org.springframework.web.bind.annotation.*;

/**
 * Classname: UserController
 * Package: org.offerproject.shortlink.admin.controller
 * Description:
 *
 * @author: 杨晨辉
 * @CreateTime: 2024/9/5 16:16
 * @version: 1.0
 */

/**
 * 用户控制层
 */
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 根据用户名查询用户信息
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDto> getUserByUsername(@PathVariable("username") String username){
        UserRespDto result = userService.getUserByUsername(username);
        return Results.success(result);
    }

    /**
     * 查询用户名是否存在
     */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username){
        return Results.success(userService.hashUsername(username));
    }

    /**
     * 注册用户
     * @param requestParam
     * @return
     */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam){
    userService.register(requestParam);
    return Results.success();
    }

    /**
     * 根据用户名修改用户
     * @param requestParam
     * @return
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam){

        userService.update(requestParam);
        return Results.success();
    }

    /**
     * 用户登录
     * @param requestParam
     * @return
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDto> loging(@RequestBody UserLoginReqDTO requestParam){
        UserLoginRespDto result = userService.login(requestParam);

        return Results.success(result);
    }

    /**
     * 检查用户是否登录
     */
    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username,@RequestParam("token") String token){

        return Results.success(userService.checkLogin(username,token));
    }

    /**
     * 用户退出登录
     * @param username
     * @param token
     */
    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username,@RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }

}
